package GraphicModels;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;

/**
 * Classe que chama o GLModel, carregando os modelos
 * @author Gustavo Buzogany Eboli
 */
public class ModelLoader {

    protected GLAutoDrawable drawable;

    public void setDrawable(GLAutoDrawable drawable)
    {
        this.drawable = drawable;
    }

    /**
     * Carrega um modelo
     * @param path Caminho para o arquivo OBJ do modelo
     * @param texturePath Caminho para o arquivo MTL das texturas do modelo
     * @return
     */
    public Model load(String path, String texturePath) {
        GLModel model = null;

        GL gl = drawable.getGL();

        try {

            FileInputStream r_path = new FileInputStream(path);
            
            BufferedReader b_read  = new BufferedReader(new InputStreamReader(r_path));
            model = new GLModel(b_read, true, texturePath, gl);
            r_path.close();
            b_read.close();

        } catch (Exception e) {
            System.out.println("LOADING ERROR: " + e);
        }

        Model aux = new Model();
        aux.setGLModel(model);

        return aux;
    }
}
